package edu.mit.csail.relo.ui;

import org.eclipse.draw2d.LayeredPane;
import org.eclipse.gef.ContextMenuProvider;
import org.eclipse.gef.EditDomain;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
import org.eclipse.jface.util.TransferDropTargetListener;

import edu.mit.csail.relo.actions.ReloContextMenuProvider;
import edu.mit.csail.relo.agent.BrowseModel;
import edu.mit.csail.relo.commands.QueueableCommandStack;
import edu.mit.csail.relo.modelBridge.ReloDoc;
import edu.mit.csail.relo.parts.ReloController;
import edu.mit.csail.relo.store.StoreUtil;


public class ReloView extends GraphicalView {
	
	// this should really be in the repository somewhere
	private static final String defaulBMPath = 
		"edu.mit.csail.relo.jdt/edu.mit.csail.relo.jdt.browse.ClassStrucBrowseModel";
	
	public static final String viewId = "edu.mit.csail.relo.ui.ReloView";
	
	public ReloView() {
		EditDomain defaultEditDomain = getEditDomain();
		defaultEditDomain.setCommandStack(new QueueableCommandStack());
		setEditDomain(defaultEditDomain);

		bm = (BrowseModel) StoreUtil.loadClass(defaulBMPath);
		bm.setRepo(StoreUtil.getDefaultStoreRepository());
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}

	ReloDoc codeBaseItems = new ReloDoc();

	@Override
	protected void initializeGraphicalViewer() {
		getGraphicalViewer().setContents(codeBaseItems);
		
		TransferDropTargetListener dragSrc = new JDTTransferDropTargetListener(getGraphicalViewer());
		getGraphicalViewer().addDropTargetListener(dragSrc);
	}

	public BrowseModel bm = null;
	@SuppressWarnings("deprecation")	// non-deprecated method are only available for Eclipse 3.2 +
	@Override
    protected void configureGraphicalViewer() {
		super.configureGraphicalViewer();
		
		final ScrollingGraphicalViewer viewer = (ScrollingGraphicalViewer)getGraphicalViewer();

		ScalableFreeformRootEditPart root = new ScalableFreeformRootEditPart() {
            @Override
            protected void createLayers(LayeredPane layeredPane) {
                super.createLayers(layeredPane);
                // enabled because needed by: ArtifactNavAidsEditPolicy
                getLayer(LayerConstants.SCALED_FEEDBACK_LAYER).setEnabled(true);
            }
        };
		//ScalableFreeformRootEditPart root = new ScalableRootEditPart();

		
		viewer.setRootEditPart(root);
		

		ContextMenuProvider provider = new ReloContextMenuProvider(viewer, getActionRegistry());
		viewer.setContextMenu(provider);
		getSite().registerContextMenu("relo.editor.contextmenu", provider, viewer);
		

        // key handling
        //KeyHandler keyHandler = viewer.getKeyHandler();
		//keyHandler.put(KeyStroke.getPressed(SWT.F2, 0),
		//		getActionRegistry().getAction(GEFActionConstants.DIRECT_EDIT));
		//keyHandler.put(KeyStroke.getPressed(SWT.F2, 0),
		//		getActionRegistry().getAction(GEFActionConstants.DIRECT_EDIT));
		//viewer.setKeyHandler(keyHandler);
        
        bm.init(viewer);
	}

	public ReloController getReloController() {
		return (ReloController) this.getGraphicalViewer().getEditPartRegistry().get(this.codeBaseItems);
	}

}
